<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-steps :active="currentTab" align-center finish-status="success">
          <el-step :title="$t('selectpre-saleproducts')" />
          <el-step :title="$t('fillinbasicinformation')" />
          <el-step :title="$t('modifyitemdetails')" />
        </el-steps>
      </div>
      <el-form
        ref="formValidate"
        v-loading="fullscreenLoading"
        class="formValidate mt20"
        :rules="ruleValidate"
        :model="formValidate"
        label-width="120px"
        @submit.native.prevent
      >
        <div v-show="currentTab === 0" style="overflow: hidden">
          <el-row :gutter="24">
            <el-col v-bind="grid2">
              <el-form-item :label="$t('selectproduct')" prop="image">
                <div class="upLoadPicBox" @click="add()">
                  <div v-if="formValidate.image" class="pictrue">
                    <img :src="formValidate.image" />
                  </div>
                  <div v-else class="upLoad">
                    <i class="el-icon-camera cameraIconfont" />
                  </div>
                </div>
              </el-form-item>
            </el-col>
          </el-row>
        </div>
        <div v-show="currentTab === 1">
          <el-row :gutter="24">
            <el-col v-bind="grid2">
              <el-form-item :label="$t('commoditymainmap')" prop="image">
                <div class="upLoadPicBox" @click="modalPicTap('1')">
                  <div v-if="formValidate.image" class="pictrue">
                    <img :src="formValidate.image" />
                  </div>
                  <div v-else class="upLoad">
                    <i class="el-icon-camera cameraIconfont" />
                  </div>
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item
                :label="$t('commoditycarouselmap')"
                prop="slider_image"
              >
                <div class="acea-row">
                  <div
                    v-for="(item, index) in formValidate.slider_image"
                    :key="index"
                    class="pictrue"
                    draggable="false"
                    @dragstart="handleDragStart($event, item)"
                    @dragover.prevent="handleDragOver($event, item)"
                    @dragenter="handleDragEnter($event, item)"
                    @dragend="handleDragEnd($event, item)"
                  >
                    <img :src="item" />
                    <i
                      class="el-icon-error btndel"
                      @click="handleRemove(index)"
                    />
                  </div>
                  <div
                    v-if="formValidate.slider_image.length < 10"
                    class="upLoadPicBox"
                    @click="modalPicTap('2')"
                  >
                    <div class="upLoad">
                      <i class="el-icon-camera cameraIconfont" />
                    </div>
                  </div>
                </div>
              </el-form-item>
            </el-col>
            <!-- 商品信息-->
            <el-col class="sp100">
              <el-form-item :label="$t('tradename')" prop="store_name">
                <el-input
                  v-model="formValidate.store_name"
                  :placeholder="$t('Please5')"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col class="sp100">
              <el-form-item :label="$t('introductiontopre')" prop="store_info">
                <el-input
                  v-model="formValidate.store_info"
                  type="textarea"
                  :rows="3"
                  :placeholder="$t('please30')"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="24">
              <el-form-item :label="$t('pre-salesettings')">
                <el-radio-group
                  v-model="formValidate.presell_type"
                  @change="wayChange"
                >
                  <el-radio :label="1" class="radio">{{
                    $t("fullpresale")
                  }}</el-radio>
                  <el-radio :label="2">{{ $t("depositpre-sale") }}</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col v-bind="grid2">
              <el-form-item :label="$t('presaleactivitydate')" required>
                <el-date-picker
                  v-model="timeVal"
                  type="datetimerange"
                  :range-separator="$t('to')"
                  :start-placeholder="$t('startdate')"
                  :end-placeholder="$t('enddate')"
                  align="right"
                  @change="onchangeTime"
                />
              </el-form-item>
            </el-col>
            <el-col v-if="formValidate.presell_type === 2" v-bind="grid2">
              <el-form-item :label="$t('finalpaymentdate')" required>
                <el-date-picker
                  v-model="timeVal2"
                  type="datetimerange"
                  :range-separator="$t('to')"
                  :start-placeholder="$t('startdate')"
                  :end-placeholder="$t('enddate')"
                  align="right"
                  @change="onchangeTime2"
                />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item :label="$t('deliverytime')" prop="delivery_type">
                <div class="acea-row">
                  <el-select
                    v-if="formValidate.presell_type === 1"
                    v-model="formValidate.delivery_type"
                    :placeholder="$t('pleaseselect')"
                    class="selWidthd1 mr20"
                  >
                    <el-option
                      v-for="item in deliveryTime"
                      :key="item.date_id"
                      :label="item.name"
                      :value="item.date_id"
                    />
                  </el-select>
                  <span v-else style="padding-right: 10px">{{
                    $t("afterfinalpayment")
                  }}</span>
                  <el-input-number
                    v-model="formValidate.delivery_day"
                    :min="1"
                    :placeholder="$t('please58')"
                    style="width: 150px"
                    class="mr20"
                  />
                  {{ $t("withindays") }}
                </div>
              </el-form-item>
            </el-col>
            <el-col v-bind="grid2">
              <el-form-item :label="$t('hrestrictions')">
                <el-input-number
                  v-model="formValidate.pay_count"
                  :min="0"
                  :placeholder="$t('Please6')"
                />
                默认“0” ，为不限制购买数量
              </el-form-item>
            </el-col>
            <el-col v-bind="grid2">
              <el-form-item :label="$t('activitystatus')">
                <el-radio-group v-model="formValidate.is_show">
                  <el-radio :label="0" class="radio">{{
                    $t("close")
                  }}</el-radio>
                  <el-radio :label="1">{{ $t("open") }}</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item :label="$t('sort')">
                <el-input-number
                  v-model="formValidate.sort"
                  :placeholder="$t('pleaseentersorting')"
                  style="width: 200px"
                />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item :label="$t('deliverymethod')" prop="delivery_way">
                <div class="acea-row">
                  <el-checkbox-group v-model="formValidate.delivery_way">
                    <el-checkbox
                      v-for="item in deliveryList"
                      :key="item.value"
                      :label="item.value"
                    >
                      {{ item.name }}
                    </el-checkbox>
                  </el-checkbox-group>
                </div>
              </el-form-item>
            </el-col>
            <el-col
              v-if="
                formValidate.delivery_way.length == 2 ||
                (formValidate.delivery_way.length == 1 &&
                  formValidate.delivery_way[0] == 2)
              "
              :span="24"
            >
              <el-form-item :label="$t('whethertoincludemail')">
                <el-radio-group v-model="formValidate.delivery_free">
                  <el-radio :label="0" class="radio">{{ $t("no") }}</el-radio>
                  <el-radio :label="1">{{ $t("yes") }}</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col
              v-if="
                formValidate.delivery_free == 0 &&
                (formValidate.delivery_way.length == 2 ||
                  (formValidate.delivery_way.length == 1 &&
                    formValidate.delivery_way[0] == 2))
              "
              :span="24"
            >
              <el-form-item :label="$t('freighttemplate')" prop="temp_id">
                <div class="acea-row">
                  <el-select
                    v-model="formValidate.temp_id"
                    :placeholder="$t('pleaseselect')"
                    class="selWidthd mr20"
                  >
                    <el-option
                      v-for="item in shippingList"
                      :key="item.shipping_template_id"
                      :label="item.name"
                      :value="item.shipping_template_id"
                    />
                  </el-select>
                  <el-button class="mr15" size="small" @click="addTem">{{
                    $t("addfreighttemplate")
                  }}</el-button>
                </div>
              </el-form-item>
            </el-col>
            <el-col v-if="labelList.length" :span="24">
              <el-form-item :label="$t('commoditylabel')">
                <el-select
                  v-model="formValidate.mer_labels"
                  multiple
                  :placeholder="$t('pleaseselect')"
                  class="selWidthd"
                >
                  <el-option
                    v-for="item in labelList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item :label="$t('platformguaranteeservice')">
                <div class="acea-row">
                  <el-select
                    v-model="formValidate.guarantee_template_id"
                    :placeholder="$t('pleaseselect')"
                    clearable
                    class="selWidthd mr20"
                  >
                    <el-option
                      v-for="item in guaranteeList"
                      :key="item.guarantee_template_id"
                      :label="item.template_name"
                      :value="item.guarantee_template_id"
                    />
                  </el-select>
                  <el-button class="mr15" size="small" @click="addServiceTem">{{
                    $t("addTemplatedesc")
                  }}</el-button>
                </div>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24" />
          <el-row :gutter="24">
            <el-col :xl="24" :lg="24" :md="24" :sm="24" :xs="24">
              <!-- 单规格表格-->
              <el-form-item v-if="formValidate.spec_type === 0">
                <el-table
                  :data="OneattrValue"
                  border
                  class="tabNumWidth"
                  size="mini"
                >
                  <el-table-column
                    align="center"
                    :label="$t('picture')"
                    min-width="80"
                  >
                    <template slot-scope="scope">
                      <div
                        class="upLoadPicBox"
                        @click="modalPicTap('1', 'dan', 'pi')"
                      >
                        <div v-if="formValidate.image" class="pictrue tabPic">
                          <img :src="scope.row.image" />
                        </div>
                        <div v-else class="upLoad tabPic">
                          <i class="el-icon-camera cameraIconfont" />
                        </div>
                      </div>
                    </template>
                  </el-table-column>
                  <el-table-column
                    align="center"
                    :label="$t('originalprice')"
                    min-width="80"
                  >
                    <template slot-scope="scope">
                      <span>{{ scope.row["price"] }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column
                    align="center"
                    :label="$t('presaleprice')"
                    min-width="80"
                  >
                    <template slot-scope="scope">
                      <el-input
                        v-model.number="scope.row['presell_price']"
                        type="number"
                        class="priceBox"
                        :min="0"
                        :max="scope.row['price']"
                        @blur="limitPrice(scope.row)"
                      />
                    </template>
                  </el-table-column>
                  <el-table-column
                    v-if="formValidate.presell_type === 2"
                    align="center"
                    :label="$t('collectadeposit')"
                    min-width="80"
                  >
                    <template slot-scope="scope">
                      <el-input
                        v-model="scope.row['down_price']"
                        type="number"
                        class="priceBox"
                        :min="0"
                        :max="scope.row['price'] * 0.2"
                        @blur="restrictedRange(scope.row)"
                      />
                    </template>
                  </el-table-column>
                  <el-table-column
                    v-if="formValidate.presell_type === 2"
                    align="center"
                    :label="$t('balancepayment')"
                    min-width="80"
                  >
                    <template slot-scope="scope">
                      <span>{{
                        scope.row["presell_price"] && scope.row["down_price"]
                          ? (
                              scope.row["presell_price"] -
                              scope.row["down_price"]
                            ).toFixed(2)
                          : scope.row["presell_price"]
                      }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column
                    align="center"
                    :label="$t('costprice')"
                    min-width="80"
                  >
                    <template slot-scope="scope">
                      <span>{{ scope.row["cost"] }}</span>
                    </template>
                  </el-table-column>

                  <el-table-column
                    align="center"
                    :label="$t('stock')"
                    min-width="80"
                  >
                    <template slot-scope="scope">
                      <span>{{ scope.row["old_stock"] }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column
                    align="center"
                    :label="$t('limit')"
                    min-width="80"
                  >
                    <template slot-scope="scope">
                      <el-input
                        v-model.number="scope.row['stock']"
                        type="number"
                        class="priceBox"
                        :max="scope.row['old_stock']"
                        :min="0"
                        @change="limitInventory(scope.row)"
                      />
                    </template>
                  </el-table-column>
                  <el-table-column
                    align="center"
                    :label="$t('articleno')"
                    min-width="80"
                  >
                    <template slot-scope="scope">
                      <span>{{ scope.row["bar_code"] }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column
                    align="center"
                    :label="$t('weightkg')"
                    min-width="80"
                  >
                    <template slot-scope="scope">
                      <span>{{ scope.row["weight"] }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column
                    align="center"
                    :label="$t('volumem')"
                    min-width="80"
                  >
                    <template slot-scope="scope">
                      <span>{{ scope.row["volume"] }}</span>
                    </template>
                  </el-table-column>
                </el-table>
              </el-form-item>
              <!-- 多规格表格-->
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-form-item
              v-if="formValidate.spec_type === 1"
              class="labeltop"
              :label="$t('specificationlist')"
            >
              <el-table
                ref="multipleSelection"
                :data="ManyAttrValue"
                tooltip-effect="dark"
                :row-key="
                  (row) => {
                    return row.id;
                  }
                "
                @selection-change="handleSelectionChange"
              >
                <el-table-column
                  align="center"
                  type="selection"
                  :reserve-selection="true"
                  min-width="50"
                />
                <template v-if="manyTabDate">
                  <el-table-column
                    v-for="(item, iii) in manyTabDate"
                    :key="iii"
                    align="center"
                    :label="manyTabTit[iii].title"
                    min-width="80"
                  >
                    <template slot-scope="scope">
                      <span class="priceBox" v-text="scope.row[iii]" />
                    </template>
                  </el-table-column>
                </template>
                <el-table-column
                  align="center"
                  :label="$t('picture')"
                  min-width="80"
                >
                  <template slot-scope="scope">
                    <div
                      class="upLoadPicBox"
                      @click="modalPicTap('1', 'duo', scope.$index)"
                    >
                      <div v-if="scope.row.image" class="pictrue tabPic">
                        <img :src="scope.row.image" />
                      </div>
                      <div v-else class="upLoad tabPic">
                        <i class="el-icon-camera cameraIconfont" />
                      </div>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column
                  align="center"
                  :label="$t('originalprice')"
                  min-width="80"
                >
                  <template slot-scope="scope">
                    <span>{{ scope.row["price"] }}</span>
                  </template>
                </el-table-column>
                <el-table-column
                  align="center"
                  :label="$t('presaleprice')"
                  min-width="80"
                >
                  <template slot-scope="scope">
                    <el-input
                      v-model.number="scope.row['presell_price']"
                      type="number"
                      class="priceBox"
                      :min="0"
                      :max="scope.row['price']"
                      @blur="limitPrice(scope.row)"
                    />
                  </template>
                </el-table-column>
                <el-table-column
                  v-if="formValidate.presell_type === 2"
                  align="center"
                  :label="$t('collectadeposit')"
                  min-width="80"
                >
                  <template slot-scope="scope">
                    <el-input
                      v-model="scope.row['down_price']"
                      type="number"
                      class="priceBox"
                      :min="0"
                      :max="scope.row['price'] * 0.2"
                    />
                  </template>
                </el-table-column>
                <el-table-column
                  v-if="formValidate.presell_type === 2"
                  align="center"
                  :label="$t('balancepayment')"
                  min-width="80"
                >
                  <template slot-scope="scope">
                    <span>{{
                      scope.row["presell_price"] && scope.row["down_price"]
                        ? scope.row["presell_price"] - scope.row["down_price"]
                        : scope.row["presell_price"]
                    }}</span>
                  </template>
                </el-table-column>
                <el-table-column
                  align="center"
                  :label="$t('costprice')"
                  min-width="80"
                >
                  <template slot-scope="scope">
                    <span>{{ scope.row["cost"] }}</span>
                  </template>
                </el-table-column>
                <el-table-column
                  align="center"
                  :label="$t('stock')"
                  min-width="80"
                >
                  <template slot-scope="scope">
                    <span>{{ scope.row["old_stock"] }}</span>
                  </template>
                </el-table-column>
                <el-table-column
                  align="center"
                  :label="$t('limit')"
                  min-width="80"
                >
                  <template slot-scope="scope">
                    <el-input
                      v-model.number="scope.row['stock']"
                      type="number"
                      class="priceBox"
                      :min="0"
                      :max="scope.row['old_stock']"
                    />
                  </template>
                </el-table-column>
                <el-table-column
                  align="center"
                  :label="$t('articleno')"
                  min-width="80"
                >
                  <template slot-scope="scope">
                    <span>{{ scope.row["bar_code"] }}</span>
                  </template>
                </el-table-column>
                <el-table-column
                  align="center"
                  :label="$t('weightkg')"
                  min-width="80"
                >
                  <template slot-scope="scope">
                    <span>{{ scope.row["weight"] }}</span>
                  </template>
                </el-table-column>
                <el-table-column
                  align="center"
                  :label="$t('volumem')"
                  min-width="80"
                >
                  <template slot-scope="scope">
                    <span>{{ scope.row["volume"] }}</span>
                  </template>
                </el-table-column>
              </el-table>
            </el-form-item>
          </el-row>
        </div>

        <!-- 商品详情-->
        <el-row v-show="currentTab === 2">
          <el-col :span="24">
            <el-form-item :label="$t('productdetails')">
              <ueditorFrom
                v-model="formValidate.content"
                :content="formValidate.content"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item style="margin-top: 30px">
          <el-button
            v-show="currentTab > 0"
            type="primary"
            class="submission"
            size="small"
            @click="handleSubmitUp"
            >{{ $t("previousstep") }}</el-button
          >
          <el-button
            v-show="currentTab == 0"
            type="primary"
            class="submission"
            size="small"
            @click="handleSubmitNest1('formValidate')"
            >{{ $t("nextstep") }}</el-button
          >
          <el-button
            v-show="currentTab == 1"
            type="primary"
            class="submission"
            size="small"
            @click="handleSubmitNest2('formValidate')"
            >{{ $t("nextstep") }}</el-button
          >
          <el-button
            v-show="currentTab === 2"
            :loading="loading"
            type="primary"
            class="submission"
            size="small"
            @click="handleSubmit('formValidate')"
            >{{ $t("submit") }}</el-button
          >
          <el-button
            v-show="currentTab === 2"
            :loading="loading"
            type="primary"
            class="submission"
            size="small"
            @click="handlePreview()"
            >{{ $t("preview") }}</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
    <!--选择商品-->
    <goods-list ref="goodsList" @getProduct="getProduct" />
    <!--添加服务保障模板-->
    <guarantee-service ref="serviceGuarantee" @get-list="getGuaranteeList" />
    <!--预览商品-->
    <div v-if="previewVisible">
      <div class="bg" @click.stop="previewVisible = false" />
      <preview-box
        v-if="previewVisible"
        ref="previewBox"
        :product-type="2"
        :preview-key="previewKey"
      />
    </div>
  </div>
</template>

<script>
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
import ueditorFrom from "@/components/ueditorFrom";
import VueUeditorWrap from "vue-ueditor-wrap";
import goodsList from "../seckill/seckillGoods/goodsList";
import guaranteeService from "@/components/serviceGuarantee/index";
import previewBox from "@/components/previewBox/index";
import {
  shippingListApi,
  templateLsitApi,
  preSaleCreateProApi,
  preSaleProDetailApi,
  productDetailApi,
  categorySelectApi,
  categoryListApi,
  categoryBrandListApi,
  preSaleUpdateProApi,
  guaranteeListApi,
  presellPreviewApi,
  productConfigApi,
  getProductLabelApi,
} from "@/api/product";
import { roterPre } from "@/settings";
const defaultObj = {
  product_id: "",
  image: "",
  slider_image: [],
  store_name: "",
  store_info: "",
  start_day: "",
  end_day: "",
  start_time: "",
  end_time: "",
  is_open_recommend: 1,
  is_open_state: 1,
  is_show: 1,
  presell_type: 1,
  keyword: "",
  brand_id: "", // 品牌id
  cate_id: "", // 平台分类id
  mer_cate_id: [], // 商户分类id
  pay_count: 0,
  integral: 0,
  sort: 0,
  is_good: 0,
  temp_id: "",
  guarantee_template_id: "",
  preSale_date: "",
  finalPayment_date: "",
  delivery_type: 1,
  delivery_day: 10,
  delivery_way: [],
  mer_labels: [],
  delivery_free: 0,
  attrValue: [
    {
      image: "",
      price: null,
      down_price: null,
      presell_price: null,
      cost: null,
      ot_price: null,
      old_stock: null,
      stock: null,
      bar_code: "",
      weight: null,
      volume: null,
    },
  ],
  attr: [],
  extension_type: 0,
  content: "",
  spec_type: 0,
  // give_coupon_ids: [],
  is_gift_bag: 0,
  // couponData: [],
};

const proOptions = [
  {
    name: "店铺推荐",
    value: "is_good",
  },
];
export default {
  name: "PresellProductAdd",
  components: {
    ueditorFrom,
    goodsList,
    VueUeditorWrap,
    guaranteeService,
    previewBox,
  },
  data() {
    return {
      pickerOptions: {
        disabledDate(time) {
          // return time.getTime() < Date.now();
          return time.getTime() > Date.now();
        },
      },
      timeVal: "",
      timeVal2: "",
      dialogVisible: false,
      product_id: "",
      multipleSelection: [],
      optionsCate: {
        value: "store_category_id",
        label: "cate_name",
        children: "children",
        emitPath: false,
      },
      roterPre: roterPre,
      selectRule: "",
      checkboxGroup: [],
      recommend: proOptions,
      tabs: [],
      fullscreenLoading: false,
      props: {
        emitPath: false,
      },
      propsMer: {
        emitPath: false,
        multiple: true,
      },
      active: 0,
      OneattrValue: [Object.assign({}, defaultObj.attrValue[0])], // 单规格
      ManyAttrValue: [Object.assign({}, defaultObj.attrValue[0])], // 多规格
      ruleList: [],
      merCateList: [], // 商户分类筛选
      categoryList: [], // 平台分类筛选
      shippingList: [], // 发货时间
      guaranteeList: [], // 服务保障模板
      deliveryList: [],
      labelList: [], // 商品标签
      deliveryTime: [
        {
          name: "支付成功",
          date_id: 1,
        },
        {
          name: "预售结束",
          date_id: 2,
        },
      ],
      spikeTimeList: [],
      BrandList: [], // 品牌
      formValidate: Object.assign({}, defaultObj),
      maxStock: "",
      addNum: 0,
      singleSpecification: {},
      multipleSpecifications: [],
      formDynamics: {
        template_name: "",
        template_value: [],
      },
      manyTabTit: {},
      manyTabDate: {},
      grid2: {
        lg: 10,
        md: 12,
        sm: 24,
        xs: 24,
      },
      // 规格数据
      formDynamic: {
        attrsName: "",
        attrsVal: "",
      },
      isBtn: false,
      manyFormValidate: [],
      images: [],
      currentTab: 0,
      isChoice: "",
      grid: {
        xl: 8,
        lg: 8,
        md: 12,
        sm: 24,
        xs: 24,
      },
      loading: false,
      ruleValidate: {
        store_name: [
          { required: true, message: this.$t("Please5"), trigger: "blur" },
        ],
        timeVal: [
          { required: true, message: this.$t("please87"), trigger: "blur" },
        ],
        timeVal2: [
          { required: true, message: this.$t("please86"), trigger: "blur" },
        ],
        mer_cate_id: [
          {
            required: true,
            message: this.$t("please91"),
            trigger: "change",
            type: "array",
            min: "1",
          },
        ],
        cate_id: [
          { required: true, message: this.$t("please37"), trigger: "change" },
        ],
        keyword: [
          { required: true, message: this.$t("please27"), trigger: "blur" },
        ],
        pay_count: [
          { required: true, message: this.$t("please36"), trigger: "blur" },
        ],
        store_info: [
          { required: true, message: this.$t("please30"), trigger: "blur" },
        ],
        temp_id: [
          { required: true, message: this.$t("please39"), trigger: "change" },
        ],
        delivery_type: [
          { required: true, message: this.$t("please160"), trigger: "change" },
        ],
        image: [
          { required: true, message: this.$t("please41"), trigger: "change" },
        ],
        slider_image: [
          {
            required: true,
            message: this.$t("uploadtheproductcarouselmap"),
            type: "array",
            trigger: "change",
          },
        ],
        delivery_way: [
          { required: true, message: this.$t("please42"), trigger: "change" },
        ],
      },
      attrInfo: {},
      keyNum: 0,
      extensionStatus: 0,
      previewVisible: false,
      previewKey: "",
      deliveryType: [],
    };
  },
  computed: {
    attrValue() {
      const obj = Object.assign({}, defaultObj.attrValue[0]);
      delete obj.image;
      return obj;
    },
    oneFormBatch() {
      const obj = [Object.assign({}, defaultObj.attrValue[0])];
      delete obj[0].bar_code;
      return obj;
    },
  },
  watch: {
    "formValidate.attr": {
      handler: function (val) {
        if (this.formValidate.spec_type === 1) this.watCh(val);
      },
      immediate: false,
      deep: true,
    },
  },
  created() {
    this.tempRoute = Object.assign({}, this.$route);
    if (this.$route.params.id && this.formValidate.spec_type === 1) {
      this.$watch("formValidate.attr", this.watCh);
    }
  },
  mounted() {
    this.formValidate.slider_image = [];
    if (this.$route.params.id) {
      this.setTagsViewTitle();
      this.getInfo(this.$route.params.id);
      this.currentTab = 1;
    } else {
      this.formValidate.attr.map((item) => {
        this.$set(item, "inputVisible", false);
      });
    }
    this.getCategorySelect();
    this.getCategoryList();
    this.getBrandListApi();
    this.getShippingList();
    this.getGuaranteeList();
    this.productCon();
    this.getLabelLst();
    this.$store.dispatch("settings/setEdit", true);
  },
  methods: {
    // 获取标签项
    getLabelLst() {
      getProductLabelApi()
        .then((res) => {
          this.labelList = res.data;
        })
        .catch((res) => {
          this.$message.error(res.message);
        });
    },
    productCon() {
      productConfigApi()
        .then((res) => {
          this.deliveryType = res.data.delivery_way.map(String);
          if (this.deliveryType.length == 2) {
            this.deliveryList = [
              { value: "1", name: this.$t("pickupatthestore") },
              { value: "2", name: this.$t("expressdelivery") },
            ];
          } else {
            if (this.deliveryType.length == 1 && this.deliveryType[0] == "1") {
              this.deliveryList = [
                { value: "1", name: this.$t("pickupatthestore") },
              ];
            } else {
              this.deliveryList = [
                { value: "2", name: this.$t("expressdelivery") },
              ];
            }
          }
        })
        .catch((res) => {
          this.$message.error(res.message);
        });
    },
    // 修改预售方式
    wayChange(e) {
      this.formValidate.presell_type = e;
    },
    // 限制预售定金最大值
    restrictedRange(row) {
      if (parseFloat(row.down_price) > row.presell_price * 0.2) {
        row.down_price = row.presell_price * 0.2;
      }
    },
    limitInventory(row) {
      if (row.stock - row.old_stock > 0) row.stock = row.old_stock;
    },
    limitPrice(row) {
      if (row.presell_price - row.price > 0) row.presell_price = row.price;
    },
    add() {
      this.$refs.goodsList.dialogVisible = true;
    },
    getProduct(data) {
      this.formValidate.image = data.src;
      this.product_id = data.id;
      console.log(this.product_id);
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 具体日期
    onchangeTime(e) {
      this.timeVal = e;
      console.log(this.moment(e[0]).format("YYYY-MM-DD HH:mm:ss"));
      this.formValidate.start_time = e
        ? this.moment(e[0]).format("YYYY-MM-DD HH:mm:ss")
        : "";
      this.formValidate.end_time = e
        ? this.moment(e[1]).format("YYYY-MM-DD HH:mm:ss")
        : "";
    },
    onchangeTime2(e) {
      this.timeVal2 = e;
      this.formValidate.final_start_time = e
        ? this.moment(e[0]).format("YYYY-MM-DD HH:mm:ss")
        : "";
      this.formValidate.final_end_time = e
        ? this.moment(e[1]).format("YYYY-MM-DD HH:mm:ss")
        : "";
    },
    setTagsViewTitle() {
      const title = this.$t("editproduct");
      const route = Object.assign({}, this.tempRoute, {
        title: `${title}-${this.$route.params.id}`,
      });
      this.$store.dispatch("tagsView/updateVisitedView", route);
    },
    onChangeGroup() {
      this.checkboxGroup.includes("is_good")
        ? (this.formValidate.is_good = 1)
        : (this.formValidate.is_good = 0);
    },
    watCh(val) {
      const tmp = {};
      const tmpTab = {};
      this.formValidate.attr.forEach((o, i) => {
        tmp["value" + i] = {
          title: o.value,
        };
        tmpTab["value" + i] = "";
      });
      this.ManyAttrValue.forEach((val, index) => {
        const key = Object.values(val.detail).sort().join("/");
        if (this.attrInfo[key]) this.ManyAttrValue[index] = this.attrInfo[key];
      });
      this.attrInfo = {};
      this.ManyAttrValue.forEach((val) => {
        this.attrInfo[Object.values(val.detail).sort().join("/")] = val;
      });
      this.manyTabTit = tmp;
      this.manyTabDate = tmpTab;
      console.log(this.manyTabTit);
      console.log(this.manyTabDate);
    },
    // 运费模板
    addTem() {
      const _this = this;
      this.$modalTemplates(0, function () {
        _this.getShippingList();
      });
    },
    // 添加服务保障模板
    addServiceTem() {
      this.$refs.serviceGuarantee.add();
    },
    // 商户分类；
    getCategorySelect() {
      categorySelectApi()
        .then((res) => {
          this.merCateList = res.data;
        })
        .catch((res) => {
          this.$message.error(res.message);
        });
    },
    // 平台分类；
    getCategoryList() {
      categoryListApi()
        .then((res) => {
          this.categoryList = res.data;
        })
        .catch((res) => {
          this.$message.error(res.message);
        });
    },
    // 品牌筛选；
    getBrandListApi() {
      categoryBrandListApi()
        .then((res) => {
          this.BrandList = res.data;
        })
        .catch((res) => {
          this.$message.error(res.message);
        });
    },
    // 获取商品属性模板；
    productGetRule() {
      templateLsitApi().then((res) => {
        this.ruleList = res.data;
      });
    },
    // 运费模板；
    getShippingList() {
      shippingListApi().then((res) => {
        this.shippingList = res.data;
      });
    },
    // 获取服务保障模板
    getGuaranteeList() {
      guaranteeListApi().then((res) => {
        this.guaranteeList = res.data;
      });
    },
    // 详情
    getInfo(id) {
      this.fullscreenLoading = true;
      this.$route.params.id
        ? preSaleProDetailApi(id)
            .then(async (res) => {
              const info = res.data;

              this.formValidate = {
                product_id: info.product_id,
                guarantee_template_id: info.product.guarantee_template_id,
                image: info.product.image,
                slider_image: info.product.slider_image,
                store_name: info.store_name,
                store_info: info.store_info,
                presell_type: info.presell_type ? info.presell_type : 1,
                delivery_type: info.delivery_type ? info.delivery_type : 1,
                delivery_day: info.delivery_day ? info.delivery_day : 10,
                start_time: info.start_time ? info.start_time : "",
                end_time: info.end_time ? info.end_time : "",
                final_start_time: info.final_start_time
                  ? info.final_start_time
                  : "",
                final_end_time: info.final_end_time ? info.final_end_time : "",
                brand_id: info.product.brand_id, // 品牌id
                cate_id: info.cate_id ? info.cate_id : "", // 平台分类id
                mer_cate_id: info.mer_cate_id, // 商户分类id
                pay_count: info.pay_count,
                sort: info.product.sort,
                is_good: info.product.is_good,
                temp_id: info.product.temp_id,
                is_show: info.is_show,
                attr: info.product.attr,
                extension_type: info.extension_type,
                content: info.product.content.content,
                spec_type: info.product.spec_type,
                is_gift_bag: info.product.is_gift_bag,
                delivery_way:
                  info.delivery_way && info.delivery_way.length
                    ? info.delivery_way.map(String)
                    : this.deliveryType,
                delivery_free: info.delivery_free ? info.delivery_free : 0,
                mer_labels:
                  info.mer_labels && info.mer_labels.length
                    ? info.mer_labels.map(Number)
                    : [],
              };
              if (this.formValidate.spec_type === 0) {
                this.OneattrValue = info.product.attrValue;
                this.OneattrValue.forEach((val, i) => {
                  this.attrInfo[Object.values(val.detail).sort().join("/")] =
                    val;
                  this.$set(
                    this.OneattrValue[i],
                    "down_price",
                    val.presellSku ? val.presellSku.down_price : 0
                  );
                  this.$set(
                    this.OneattrValue[i],
                    "presell_price",
                    val.presellSku ? val.presellSku.presell_price : val.price
                  );
                  this.$set(
                    this.OneattrValue[i],
                    "stock",
                    val.presellSku ? val.presellSku.stock : val.old_stock
                  );
                });
                this.singleSpecification = JSON.parse(
                  JSON.stringify(info.product.attrValue)
                );
                this.formValidate.attrValue = this.OneattrValue;
              } else {
                const arr = [];
                this.ManyAttrValue = info.product.attrValue;
                this.ManyAttrValue.forEach((val, i) => {
                  this.attrInfo[Object.values(val.detail).sort().join("/")] =
                    val;
                  this.$set(
                    this.ManyAttrValue[i],
                    "down_price",
                    val.presellSku ? val.presellSku.down_price : 0
                  );
                  this.$set(
                    this.ManyAttrValue[i],
                    "presell_price",
                    val.presellSku ? val.presellSku.presell_price : val.price
                  );
                  this.$set(
                    this.ManyAttrValue[i],
                    "stock",
                    val.presellSku ? val.presellSku.stock : val.old_stock
                  );
                  if (val.presellSku) {
                    this.multipleSpecifications = JSON.parse(
                      JSON.stringify(info.product.attrValue)
                    );
                    arr.push(val);
                  }
                });
                this.multipleSpecifications = JSON.parse(JSON.stringify(arr));
                this.$nextTick(() => {
                  arr.forEach((row) => {
                    this.$refs.multipleSelection.toggleRowSelection(row, true);
                  });
                });
                this.formValidate.attrValue = this.multipleSelection;
              }

              console.log(this.ManyAttrValue);
              this.fullscreenLoading = false;
              this.timeVal = [
                new Date(this.formValidate.start_time),
                new Date(this.formValidate.end_time),
              ];
              this.timeVal2 = [
                new Date(this.formValidate.final_start_time),
                new Date(this.formValidate.final_end_time),
              ];
              this.$store.dispatch("settings/setEdit", true);
            })
            .catch((res) => {
              this.fullscreenLoading = false;
              this.$message.error(res.message);
            })
        : productDetailApi(id)
            .then(async (res) => {
              const info = res.data;
              this.formValidate = {
                product_id: info.product_id,
                image: info.image,
                slider_image: info.slider_image,
                store_name: info.store_name,
                store_info: info.store_info,
                presell_type: 1,
                delivery_type: info.delivery_type ? info.delivery_type : 1,
                delivery_day: info.delivery_day ? info.delivery_day : 10,
                start_time: "",
                end_time: "",
                final_start_time: "",
                final_end_time: "",
                brand_id: info.brand_id, // 品牌id
                cate_id: info.cate_id, // 平台分类id
                mer_cate_id: info.mer_cate_id, // 商户分类id
                pay_count: info.pay_count ? info.paycount : 0,
                sort: info.sort,
                is_good: info.is_good,
                temp_id: info.temp_id,
                guarantee_template_id: info.guarantee_template_id,
                is_show: info.is_show,
                attr: info.attr,
                extension_type: info.extension_type,
                content: info.content,
                spec_type: info.spec_type,
                is_gift_bag: info.is_gift_bag,
                delivery_way:
                  info.delivery_way && info.delivery_way.length
                    ? info.delivery_way.map(String)
                    : this.deliveryType,
                delivery_free: info.delivery_free ? info.delivery_free : 0,
                mer_labels:
                  info.mer_labels && info.mer_labels.length
                    ? info.mer_labels.map(Number)
                    : [],
              };
              this.timeVal = this.timeVal2 = [];
              if (this.formValidate.spec_type === 0) {
                this.OneattrValue = info.attrValue;
                this.OneattrValue.forEach((val, i) => {
                  this.$set(this.OneattrValue[i], "down_price", 0);
                  this.$set(
                    this.OneattrValue[i],
                    "presell_price",
                    this.OneattrValue[i].price
                  );
                });
                this.singleSpecification = JSON.parse(
                  JSON.stringify(info.attrValue)
                );
                this.formValidate.attrValue = this.OneattrValue;
              } else {
                this.ManyAttrValue = info.attrValue;
                this.multipleSpecifications = JSON.parse(
                  JSON.stringify(info.attrValue)
                );
                this.ManyAttrValue.forEach((val, i) => {
                  this.attrInfo[Object.values(val.detail).sort().join("/")] =
                    val;
                  this.$set(this.ManyAttrValue[i], "down_price", 0);
                  this.$set(
                    this.ManyAttrValue[i],
                    "presell_price",
                    this.ManyAttrValue[i].price
                  );
                });
                this.multipleSelection = info.attrValue;
                this.$nextTick(() => {
                  info.attrValue.forEach((row) => {
                    this.$refs.multipleSelection.toggleRowSelection(row, true);
                  });
                });
                //    this.formValidate.attrValue = this.multipleSelection;
                // console.log(this.multipleSelection)
              }
              if (this.formValidate.is_good === 1) {
                this.checkboxGroup.push("is_good");
              }
              this.fullscreenLoading = false;
            })
            .catch((res) => {
              this.fullscreenLoading = false;
              this.$message.error(res.message);
            });
    },
    handleRemove(i) {
      this.formValidate.slider_image.splice(i, 1);
    },
    // 点击商品图
    modalPicTap(tit, num, i) {
      const _this = this;
      const attr = [];
      this.$modalUpload(function (img) {
        if (tit === "1" && !num) {
          _this.formValidate.image = img[0];
          _this.OneattrValue[0].image = img[0];
        }
        if (tit === "2" && !num) {
          img.map((item) => {
            attr.push(item.attachment_src);
            _this.formValidate.slider_image.push(item);
            if (_this.formValidate.slider_image.length > 10) {
              _this.formValidate.slider_image.length = 10;
            }
          });
        }
        if (tit === "1" && num === "dan") {
          _this.OneattrValue[0].image = img[0];
        }
        if (tit === "1" && num === "duo") {
          _this.ManyAttrValue[i].image = img[0];
        }
        if (tit === "1" && num === "pi") {
          _this.oneFormBatch[0].image = img[0];
        }
      }, tit);
    },
    handleSubmitUp() {
      if (this.currentTab-- < 0) this.currentTab = 0;
    },
    handleSubmitNest1(name) {
      if (!this.formValidate.image) {
        this.$message.warning(this.$t("pleaseselectaproduct"));
        return;
      } else {
        this.currentTab++;
        if (!this.$route.params.id) this.getInfo(this.product_id);
      }
    },
    handleSubmitNest2(name) {
      if (this.formValidate.spec_type === 1) {
        this.formValidate.attrValue = this.multipleSelection;
      } else {
        this.formValidate.attrValue = this.OneattrValue;
      }
      console.log(this.formValidate);
      this.$refs[name].validate((valid) => {
        if (valid) {
          if (
            !this.formValidate.store_name ||
            !this.formValidate.store_info ||
            !this.formValidate.image ||
            !this.formValidate.slider_image
          ) {
            this.$message.warning(this.$t("tips2"));
            return;
          }
          if (
            !this.formValidate.attrValue ||
            this.formValidate.attrValue.length === 0
          ) {
            this.$message.warning(this.$t("pleaseselectthhe"));
            return;
          }
          if (!this.formValidate.delivery_day) {
            this.$message.warning(this.$t("please172"));
            return;
          }
          this.currentTab++;
        }
      });
    },
    // 提交
    handleSubmit(name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          this.$store.dispatch("settings/setEdit", false);
          this.fullscreenLoading = true;
          this.loading = true;
          delete this.formValidate.preSale_date;
          delete this.formValidate.finalPayment_date;
          console.log(this.formValidate);
          if (this.$route.params.id) {
            console.log(this.ManyAttrValue);
            if (this.formValidate.presell_type === 1) {
              this.formValidate.final_start_time =
                this.formValidate.final_end_time = "";
            }
            preSaleUpdateProApi(this.$route.params.id, this.formValidate)
              .then(async (res) => {
                this.fullscreenLoading = false;
                this.$message.success(res.message);
                this.$router.push({
                  path: this.roterPre + "/marketing/presell/list",
                });
                this.$refs[name].resetFields();
                this.formValidate.slider_image = [];
                this.loading = false;
              })
              .catch((res) => {
                this.fullscreenLoading = false;
                this.loading = false;
                this.$message.error(res.message);
              });
          } else {
            preSaleCreateProApi(this.formValidate)
              .then(async (res) => {
                this.fullscreenLoading = false;
                this.$message.success(res.message);
                this.$router.push({
                  path: this.roterPre + "/marketing/presell/list",
                });
                this.$refs[name].resetFields();
                this.formValidate.slider_image = [];
                this.loading = false;
              })
              .catch((res) => {
                this.fullscreenLoading = false;
                this.loading = false;
                this.$message.error(res.message);
              });
          }
        } else {
          if (
            !this.formValidate.store_name ||
            !this.formValidate.store_info ||
            !this.formValidate.image ||
            !this.formValidate.slider_image
          ) {
            this.$message.warning(this.$t("tips2"));
          }
        }
      });
    },
    // 预览
    handlePreview() {
      delete this.formValidate.preSale_date;
      delete this.formValidate.finalPayment_date;
      if (this.formValidate.presell_type === 1) {
        this.formValidate.final_start_time = this.formValidate.final_end_time =
          "";
      }
      presellPreviewApi(this.formValidate)
        .then(async (res) => {
          this.previewVisible = true;
          this.previewKey = res.data.preview_key;
        })
        .catch((res) => {
          this.$message.error(res.message);
        });
    },
    // 表单验证
    validate(prop, status, error) {
      if (status === false) {
        this.$message.warning(error);
      }
    },
    // 移动
    handleDragStart(e, item) {
      this.dragging = item;
    },
    handleDragEnd(e, item) {
      this.dragging = null;
    },
    handleDragOver(e) {
      e.dataTransfer.dropEffect = "move";
    },
    handleDragEnter(e, item) {
      e.dataTransfer.effectAllowed = "move";
      if (item === this.dragging) {
        return;
      }
      const newItems = [...this.formValidate.slider_image];
      const src = newItems.indexOf(this.dragging);
      const dst = newItems.indexOf(item);
      newItems.splice(dst, 0, ...newItems.splice(src, 1));
      this.formValidate.slider_image = newItems;
    },
  },
};
</script>

<style lang="scss" scoped>
.bg {
  z-index: 100;
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
}
.priceBox {
  ::v-deepinput {
    padding-right: 0;
  }
}
.sp100 {
  width: 100%;
}
.proCoupon {
  ::v-deep.el-form-item__content {
    margin-top: 5px;
  }
}

.tabPic {
  width: 40px !important;
  height: 40px !important;

  img {
    width: 100%;
    height: 100%;
  }
}

.noLeft {
  ::v-deep.el-form-item__content {
    margin-left: 0 !important;
  }
}

.tabNumWidth {
  ::v-deep.el-input-number--medium {
    width: 121px !important;
  }

  ::v-deep.el-input-number__increase {
    width: 20px !important;
    font-size: 12px !important;
  }

  ::v-deep.el-input-number__decrease {
    width: 20px !important;
    font-size: 12px !important;
  }

  ::v-deep.el-input-number--medium .el-input__inner {
    padding-left: 25px !important;
    padding-right: 25px !important;
  }

  ::v-deep thead {
    line-height: normal !important;
  }

  ::v-deep .el-table .cell {
    line-height: normal !important;
  }
}

.selWidth {
  width: 100%;
}

.selWidthd {
  width: 300px;
}

.selWidthd1 {
  width: 200px;
}

.button-new-tag {
  height: 28px;
  line-height: 26px;
  padding-top: 0;
  padding-bottom: 0;
}

.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}

.pictrue {
  width: 60px;
  height: 60px;
  border: 1px dotted rgba(0, 0, 0, 0.1);
  margin-right: 10px;
  position: relative;
  cursor: pointer;

  img {
    width: 100%;
    height: 100%;
  }
}

.btndel {
  position: absolute;
  z-index: 1;
  width: 20px !important;
  height: 20px !important;
  left: 46px;
  top: -4px;
}

.labeltop {
  ::v-deep.el-form-item__label {
    float: none !important;
    display: inline-block !important;
    margin-left: 120px !important;
    width: auto !important;
  }
}
</style>
